package com.wzt.lianfirecontrol.adapter.xuncha;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.XunChaBuildingModel;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.XunChaFloorModel;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.XunChaPlanModel;
import com.wzt.lianfirecontrol.utils.Utils;

public class XunChaSelectBuildAdapter extends BaseRecyclerAdapter<XunChaBuildingModel> {

    private Context context;

    /**
     * 是否为完成页面
     */
    private boolean isCompletePage = false;

    private XunChaPlanModel xunChaPlanModel;

    public XunChaPlanModel getXunChaPlanModel() {
        return xunChaPlanModel;
    }

    public void setXunChaPlanModel(XunChaPlanModel xunChaPlanModel) {
        this.xunChaPlanModel = xunChaPlanModel;
    }

    public boolean isComplete() {
        return isCompletePage;
    }

    public void setComplete(boolean complete) {
        isCompletePage = complete;
    }

    public XunChaSelectBuildAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_xuncha_select_build, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XunChaBuildingModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private LinearLayout ll_title;
        private TextView tv_location;
        private TextView tv_status;
        private ImageView iv_expend;
        private RecyclerView rlv_floor_list;
        private XunChaSelectFloorAdapter xunChaSelectFloorAdapter;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.ll_title = itemView.findViewById(R.id.ll_title);
        myViewHolder.tv_location = itemView.findViewById(R.id.tv_location);
        myViewHolder.tv_status = itemView.findViewById(R.id.tv_status);
        myViewHolder.iv_expend = itemView.findViewById(R.id.iv_expend);
        myViewHolder.rlv_floor_list = itemView.findViewById(R.id.rlv_floor_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        myViewHolder.rlv_floor_list.setLayoutManager(linearLayoutManager);
        myViewHolder.xunChaSelectFloorAdapter = new XunChaSelectFloorAdapter(context);
        myViewHolder.xunChaSelectFloorAdapter.setComplete(isCompletePage);
        myViewHolder.rlv_floor_list.setAdapter(myViewHolder.xunChaSelectFloorAdapter);
    }

    private void setItemData(final MyViewHolder viewHolder, final XunChaBuildingModel data, int realPosition) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (isCompletePage){
            layoutParams.setMargins(0, 0, 0, 0);
        }else {
            if (realPosition == 0) {
                layoutParams.setMargins(0, Utils.dip2px(context, 15), 0, Utils.dip2px(context, 15));
            } else {
                layoutParams.setMargins(0, 0, 0, Utils.dip2px(context, 15));
            }
        }

        viewHolder.tv_location.setText(data.getName());
        if ("0".equals(data.getDownExpand())){
            viewHolder.iv_expend.setImageResource(R.mipmap.up_expand);
            viewHolder.rlv_floor_list.setVisibility(View.VISIBLE);
        }else {
            viewHolder.iv_expend.setImageResource(R.mipmap.down_expand);
            viewHolder.rlv_floor_list.setVisibility(View.GONE);
        }
        viewHolder.xunChaSelectFloorAdapter.clear();
        viewHolder.xunChaSelectFloorAdapter.setListPosition(realPosition);
        viewHolder.xunChaSelectFloorAdapter.addDatas(data.getChildren());
        viewHolder.xunChaSelectFloorAdapter.setOnItemClickListener(new OnItemClickListener<XunChaFloorModel>() {
            @Override
            public void onItemClick(View v, int position, XunChaFloorModel model) {
                if ("2".equals(model.getStatus()) && !isCompletePage){
                    // 已完成
                    // 防火巡查检查列表点击动作
                    Bundle skipToBundle = new Bundle();
                    skipToBundle.putSerializable("data", xunChaPlanModel);
                    skipToBundle.putBoolean("isComplete", true);
                    BannerModel bannerModel = new BannerModel();
                    bannerModel.setActionType(ConstData.SHOWFRAG);
                    bannerModel.setActionValue(ConstData.XUNCHACOMPLETE);
                    bannerModel.setActionTitle( "巡查详情");
                    ((BaseActivity)context).setClickAction(bannerModel, skipToBundle);

                    return;
                }
                if ("0".equals(model.getIsAllSelect())){
                    if ("0".equals(model.getCurrentSelectStatus())){
                        for (XunChaFloorModel xunChaFloorModel :data.getChildren()){
                            xunChaFloorModel.setCurrentSelectStatus("1");
                        }
                    }else {
                        for (XunChaFloorModel xunChaFloorModel :data.getChildren()){
                            xunChaFloorModel.setCurrentSelectStatus("0");
                        }
                    }

                }else {
                    if ("0".equals(model.getCurrentSelectStatus())){
                        model.setCurrentSelectStatus("1");
                    }else {
                        model.setCurrentSelectStatus("0");
                    }
                }
                viewHolder.xunChaSelectFloorAdapter.notifyDataSetChanged();
            }
        });
        viewHolder.ll_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("0".equals(data.getDownExpand())) {
                    data.setDownExpand("1");
                }else {
                    data.setDownExpand("0");
                }
                notifyDataSetChanged();
            }
        });

        if (!isCompletePage) {
            viewHolder.tv_status.setVisibility(View.VISIBLE);
            if ("2".equals(data.getRecordStatus())) {
                viewHolder.tv_status.setText("已完成");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.main_color));
            } else if ("0".equals(data.getRecordStatus())) {
                viewHolder.tv_status.setText("待检查");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.change_status_color));
            } else if ("1".equals(data.getRecordStatus())){
                viewHolder.tv_status.setText("检查中");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.look_status_color));
            }else {
                viewHolder.tv_status.setText("已过期");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.hint_text_color));
            }
        }else {
            viewHolder.tv_status.setVisibility(View.GONE);
        }
    }

}
